/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/

#ifndef INCLUDE_SHADER_TASTER_EDGE
#define INCLUDE_SHADER_TASTER_EDGE





#ifdef __cplusplus

// GLSL Type
using uint = unsigned int;

#define inout

#else



vec3 Normal;
uint primitiveID;
S_FaceElement face;

float lineWidth[3];
vec4 _point[3];
vec4 _color[3];


void f_element_build(in vec4 color, in vec2 uv) {
	outPrimitiveID = primitiveID;

	Element.MaterialID = face.MaterialID;
	Element.MaterialSlot = face.MaterialSlot;
	Element.Offset_MeshAttr = vFaceAttr[0].Offset_MeshAttr;
	Element.Offset_MaterialAttr = vFaceAttr[0].Offset_MaterialAttr;

	outCamera[0] = vCamera[0][0];
	outCamera[1] = vCamera[0][1];
	outCamera[2] = vCamera[0][2];
	outCamera[3] = vCamera[0][3];


	outColor = color;
	outNormal = Normal;

	outUV[0] = uv;
}

vec2 f_point_rot(vec2 point, float angle) {
	mat2 rot;
	rot[0] = vec2(cos(angle), -sin(angle));
	rot[1] = vec2(sin(angle), cos(angle));
	return rot * point;
}

void f_line_build(in vec4 pointA, in vec4 pointB, in vec4 colorA, in vec4 colorB, float widthA, float widthB) {
	vec3 dir = pointA.xyz - pointB.xyz;
	float len = length(dir);
	//int num = int(len / width);
	widthA = clamp(widthA, 0, 0.3);
	widthB = clamp(widthB, 0, 0.3);
	float uvWeigth_A = (widthA * 0.5) / (len + (widthA + widthB) * 0.5);
	float uvWeigth_B = (widthB * 0.5) / (len + (widthA + widthB) * 0.5);
	vec2 uv = vec2(-0.5, 0);

	vec3 n = normalize(cross(dir, vec3(0, 0, 1)));
	vec3 sv = n * widthA * 0.5;

	for (uint i = 0; i < 4; ++i) {
		gl_Position = vec4(sv.xy + pointA.xy, pointA.zw);
		gVert = vVert[0];

		f_element_build(colorA, vec2(uv.x + 0.5, 0.5 - abs(uv.y)) * vec2(1, 2 * uvWeigth_A));
		EmitVertex();


		sv.xy = f_point_rot(sv.xy, radians(-45));
		gl_Position = vec4(sv.xy + pointA.xy, pointA.zw);
		gVert = vVert[0];

		uv = f_point_rot(uv, radians(-45));
		f_element_build(colorA, vec2(uv.x + 0.5, 0.5 - abs(uv.y)) * vec2(1, 2 * uvWeigth_A));
		EmitVertex();


		gl_Position = pointA;
		gVert = vVert[0];

		f_element_build(colorA, vec2(0.5, 1 * uvWeigth_A));
		EmitVertex();
		EndPrimitive();
	}


	sv = n * widthA * 0.5;
	gl_Position = vec4(sv.xy + pointA.xy, pointA.zw);
	f_element_build(colorA, vec2(0, uvWeigth_A));
	EmitVertex();
	gl_Position = vec4(-sv.xy + pointA.xy, pointA.zw);
	f_element_build(colorA, vec2(1, uvWeigth_A));
	EmitVertex();

	sv = n * widthB * 0.5;
	gl_Position = vec4(sv.xy + pointB.xy, pointB.zw);
	f_element_build(colorB, vec2(0, 1 - uvWeigth_B));
	EmitVertex();
	gl_Position = vec4(-sv.xy + pointB.xy, pointB.zw);
	f_element_build(colorB, vec2(1, 1 - uvWeigth_B));
	EmitVertex();
	EndPrimitive();


	uv = vec2(0.5, 0);
	sv.xy = -sv.xy;
	for (uint i = 0; i < 4; ++i) {
		gl_Position = vec4(sv.xy + pointB.xy, pointB.zw);
		gVert = vVert[0];

		f_element_build(colorB, vec2(uv + vec2(0.5, 0)) * vec2(1, 2 * uvWeigth_B) + vec2(0, len));
		EmitVertex();


		uv = f_point_rot(uv, radians(-45));
		sv.xy = f_point_rot(sv.xy, radians(-45));
		gl_Position = vec4(sv.xy + pointB.xy, pointB.zw);
		gVert = vVert[0];

		f_element_build(colorB, vec2(uv + vec2(0.5, 0)) * vec2(1, 2 * uvWeigth_B) + vec2(0, len));
		EmitVertex();


		gl_Position = pointB;
		gVert = vVert[0];

		f_element_build(colorB, vec2(0.5, 1 - uvWeigth_B));
		EmitVertex();
		EndPrimitive();
	}
}


#endif










#endif //INCLUDE_SHADER_TASTER_EDGE










